package com.saver.modules.sys.service;

import com.saver.common.db.JdbcTool;
import com.saver.common.utils.PageUtils;
import com.saver.common.utils.Query;
import com.saver.common.utils.R;
import com.saver.modules.sys.dao.SysNoticeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SysNoticeService {

    @Autowired
    private JdbcTool jdbcTool;

    @Autowired
    private SysNoticeDao sysNoticeDao;


    public R queryPage( Map<String, Object> params) {
        Query query = new Query(params);
        String sql = "select id,title,state,(select name FROM sys_user su where su.user_id=sn.create_user_id) createUserName,create_date from sys_notice sn";
        String q = (String) query.get("q");
        if (q != null && !q.equals("")) {
            sql = sql + " where (sn.title like '%" + q + "%') ";
        }
        PageUtils pageUtils = jdbcTool.queryPage(sql, query);
        return R.ok().put("page", pageUtils);
    }

    public R show() {
        List<Map<String, Object>> list = jdbcTool.list("select id,title,content from sys_notice where state = 1");
        return R.ok().put("data", list);
    }

    @Transactional
    public R deleteBatch(Integer[] ids){
        for (Integer id : ids) {
            sysNoticeDao.deleteById(id);
        }
        return R.ok();
    }
}
